package com.schoolcloud.social.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schoolcloud.common.model.PageDTO;
import com.schoolcloud.common.model.UserInfo;
import com.schoolcloud.common.util.UserContext;
import com.schoolcloud.social.entity.SocEventParticipants;
import com.schoolcloud.social.query.EventParticipantsQuery;
import com.schoolcloud.social.service.SocEventParticipantsService;
import com.schoolcloud.social.mapper.SocEventParticipantsMapper;
import com.schoolcolud.daomybatisplus.dto.MpPageUtil;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author hongmizfb
 * @description 针对表【soc_event_participants(活动参与者记录)】的数据库操作Service实现
 * @createDate 2025-03-02 10:36:56
 */
@Service
public class SocEventParticipantsServiceImpl extends ServiceImpl<SocEventParticipantsMapper, SocEventParticipants>
        implements SocEventParticipantsService {

    @Override
    public PageDTO<SocEventParticipants> getPage(EventParticipantsQuery eventParticipantsQuery) {
        Integer eventId = eventParticipantsQuery.getEventId();
        Integer eventUserId = eventParticipantsQuery.getEventUserId();
        Page<SocEventParticipants> page = lambdaQuery()
                .eq(eventId != null, SocEventParticipants::getEventId, eventId)
                .page(eventParticipantsQuery.toMpPage());
        return MpPageUtil.of(page, SocEventParticipants.class);
    }

    @Override
    public void addEventParticipants(SocEventParticipants socEventParticipants) {
        if (socEventParticipants.getUserId() == null){
            socEventParticipants.setUserId(UserContext.getUserToInteger());
        }
        socEventParticipants.setAppliedTime(LocalDateTime.now());
        save(socEventParticipants);
    }

    @Override
    public void updateEventParticipants(SocEventParticipants socEventParticipants) {
        updateById(socEventParticipants);
    }

    @Override
    public Long getUserJoinEventCount(Integer userId) {
        return lambdaQuery()
                .eq(SocEventParticipants::getUserId, userId)
                .count();
    }

    @Override
    public List<SocEventParticipants> getUserJoinEvent(String joinerId) {
        return lambdaQuery()
                .eq(SocEventParticipants::getUserId, joinerId)
                .list();
    }

    @Override
    public Long getJoinerCount(Integer eventId) {
        return baseMapper.getJoinerCount(eventId);
    }

    @Override
    public Long getEventJoinerCount(Integer eventId) {
        return lambdaQuery().eq(SocEventParticipants::getEventId, eventId).count();
    }

}




